import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-11-16
 * Time: 14:33
 */
class Message {
    public String name;
    public String gender;

    public Message(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Message{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}
@WebServlet("/test")
public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("name","zhangsan");
        resp.addCookie(cookie);
        resp.getWriter().write("ok");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        HttpSession httpSession = req.getSession(true);
        resp.getWriter().write(String.valueOf(httpSession.isNew()));
        Message message = new Message("zhangsan", "man");
        httpSession.setAttribute("cookie1", message);
        Message m = (Message)httpSession.getAttribute("cookie1");
        resp.getWriter().write(m.toString());
    }
}
